#include "Btn.h"
#include <QGraphicsEffect>

Btn::Btn(const QPixmap& pixmap, QGraphicsItem* parent)
    :QGraphicsPixmapItem(pixmap, parent)
{
    m_originalPixmap = pixmap; // 保存原始图像
    setAcceptHoverEvents(true); // 启用悬停事件
}
// 启用悬停事件
void Btn::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
    Q_UNUSED(event);
    QGraphicsColorizeEffect* effect = new QGraphicsColorizeEffect;
    effect->setColor(Qt::red); // 悬停时染色为红色
    effect->setStrength(0.3);
    setGraphicsEffect(effect);
}
void Btn::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
{
    Q_UNUSED(event);
    setGraphicsEffect(nullptr); // 移除效果
}
// 鼠标点击事件
void Btn::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
    QGraphicsPixmapItem::mousePressEvent(event);
    if (boundingRect().contains(event->pos()))
    {
        emit clicked(); // 发送点击信号
    }
}
